package cn.qy.kotlin.base

import cn.qy.kotlin.base.ch10.Player

/**
 * 定义类
 */
fun main() {
    val playerCh10 = Player()
    playerCh10.name = "estragon "
    println(playerCh10.name + "TheBrave")

    val dice = Dice()
    println(dice.rolledValue)
    println(dice.rolledValue)
    println(dice.rolledValue)

    playerCh10.castFireball()

    // com.bignerdranch.nyethack.Player status
    printPlayerStatus(playerCh10)

    Player222().printWeaponName()
}

private fun printPlayerStatus(playerCh10: Player) {
    println("(Aura: ${playerCh10.auraColor()}) (Blessed: ${if (playerCh10.isBlessed) "YES" else "NO"})")
    println("${playerCh10.name} ${playerCh10.formatHealthStatus()}")
}

class Dice() {
    // 计算属性是通过一个覆盖的 get 和（或）set 运算符来定义的，这时 field 就不需要了。也就是说，Kotlin 在碰到这种情况时就不会产生 field
    val rolledValue get() = (1..6).shuffled().first()
}

class Weapon(val name: String)

class Player222 {
    // 防范竞态条件示例
    var weapon: Weapon? = Weapon("Ebony Kris")
    fun printWeaponName() {
        weapon?.let { println( it.name) }
    }
}